package com.xbongbong.sys.help;

import com.alibaba.fastjson.JSON;
import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.constant.TimeConstant;
import com.xbongbong.paas.redis.PaasRedisHelper;
import com.xbongbong.paas.redis.RedisConstant;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.DateTimeUtil;
import com.xbongbong.saas.enums.CompanyConfigEnum;
import com.xbongbong.sys.domain.entity.CompanyConfigEntity;
import com.xbongbong.sys.model.CompanyConfigModel;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;

/**
 * @author 吴峰
 * @date 2020/08/31 14:42
 */
@Component
public class PackageWhitelistHelp {


    @Resource
    private CompanyConfigModel companyConfigModel;
    @Resource
    private PaasRedisHelper paasRedisHelper;

    public Set<String> getPackageWhitelist(String corpid) throws XbbException {
        Set<String> corpidSet = paasRedisHelper.getValue4Set(RedisConstant.KEY_COMPANY_CONFIG, CompanyConfigEnum.PACKAGE_WHITELIST.getAlias());
        if (Objects.nonNull(corpidSet)) {
            return corpidSet;
        }
        CompanyConfigEntity companyConfigEntity = companyConfigModel.getByConfigAlias( CompanyConfigEnum.PACKAGE_WHITELIST.getAlias(), PaasConstant.DEFAULT_DATA_CORPID);
        if (Objects.isNull(companyConfigEntity)) {
            return new HashSet<>();
        }
        List<String> corpidList = JSON.parseArray(companyConfigEntity.getConfigValue(), String.class);
        corpidSet = new HashSet<>(corpidList);
        paasRedisHelper.setValue4Set(RedisConstant.KEY_COMPANY_CONFIG, CompanyConfigEnum.PACKAGE_WHITELIST.getAlias(), corpidSet, TimeConstant.SECONDS_PER_HOUR);
        return corpidSet;
    }

    /**
     * 如果套餐起始时间大于套餐降级上线时间，那么该公司需要从套餐白名单中剔除
     * @param corpid
     * @param feeStartTime
     * @throws XbbException
     */
    public void setPackageWhitelist(String corpid, Long feeStartTime) throws XbbException {
        long time = DateTimeUtil.getInt("2020-09-15 00:00:00");
        if (Objects.nonNull(feeStartTime) && feeStartTime > time) {
            // 如果套餐起始时间大于套餐降级上线时间，那么该公司需要从套餐白名单中剔除
            CompanyConfigEntity companyConfigEntity = companyConfigModel.getByConfigAlias( CompanyConfigEnum.PACKAGE_WHITELIST.getAlias(), PaasConstant.DEFAULT_DATA_CORPID);
            if (Objects.nonNull(companyConfigEntity)) {
                List<String> corpidList = JSON.parseArray(companyConfigEntity.getConfigValue(), String.class);
                corpidList.remove(corpid);
                Set<String> corpidSet = new HashSet<>(corpidList);
                companyConfigModel.save(PaasConstant.DEFAULT_DATA_CORPID, CompanyConfigEnum.PACKAGE_WHITELIST.getAlias(), CompanyConfigEnum.PACKAGE_WHITELIST.getName(), JSON.toJSONString(corpidSet));
                paasRedisHelper.setValue4Set(RedisConstant.KEY_COMPANY_CONFIG, CompanyConfigEnum.PACKAGE_WHITELIST.getAlias(), corpidSet, TimeConstant.SECONDS_PER_HOUR);
            }
        }

    }


}
